home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: separate compilation
- Date: 02 Mar 1996 20:06:22 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Mar2130622@qcd.lanl.gov>
- References: <1996Mar2.164559.24931@dcs.warwick.ac.uk>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: D.C.Molero@dcs.warwick.ac.uk's message of Sat, 2 Mar 1996 16:45:59 GMT
-
- In article <1996Mar2.164559.24931@dcs.warwick.ac.uk>
- D.C.Molero@dcs.warwick.ac.uk (Daniel Castillo Molero) writes:
-
- <snip>
- DCM: /* declare the structure side as extern to avoid having to redeclare it,
- DCM: since it is already declared in file2.c
- DCM: */
- DCM: extern struct side;
-
- This makes no sense. extern helps specify the linkage of identifiers,
- struct tags have no linkage. In other words, just specifying `extern
- struct side;' does not mean that the compiler will search through all
- your files to find what struct side is.
-
- The contents of a struct need to be known by the compiler in some
- cases. Use `extern' for variables and function names if you want, not
- for struct tags.
-
- The common way to solve the problem is to have a small file `side.h'
- which contains the declaration
-
- struct side {
- int a;
- struct side *next;
- }
-
- and then say #include "side.h" in whichever file needs it.
-
-
- DCM:
- DCM: int main(void) {
- DCM: struct side *tmp;
- DCM: /* first_side is declared in file2.c and points to the first element
- DCM: of the list.
- DCM: */
- DCM: extern struct side *first_side;
-
- It is best to put this line in a .h file as well. Then you can include
- the same .h file both here and in file2.c, and the compiler will check
- that the declaration matches the definition.
-
- Moreover, many people consider that extern declarations should not
- appear inside blocks: there are cases where they are useful. In this
- particular case, I would rather declare it ouside all functions.
-
- DCM: /* print the three elements */
- DCM:
- DCM: for (tmp = first_side; tmp; tmp = tmp->next)
- DCM: printf("side: %d\n", tmp->a);
-
- To use printf you have to #include <stdio.h> at the top of your file.
-
- So you never actually call any of the functions in the other files?
- Remember all that your program done is evaluate main() afetr global
- initializations: if mai calls nothing else, nothing else is done.
-
- DCM:
- DCM: return 0;
- DCM: }
- DCM:
- DCM:
- DCM: file2.c
- DCM: -------
- DCM:
- DCM: /* type of each element of the linked list */
- DCM: struct side {
- DCM: int a;
- DCM: struct side *next;
- DCM: };
- DCM:
- DCM: void store_side(struct side* i, struct side** first_side);
-
- It is best to put this line in a .h file as well. Then you can include
- the same .h file both here and in file3.c, and the compiler will check
- that the declaration matches the definition.
-
- DCM:
- DCM: void create_list(void)
- DCM: {
- DCM: struct side *i;
- DCM: struct side *first_side;
-
- Hmmm. You expect all your local variables in every file to be visible
- in every other file? Writing programs would become impossible then:
- one would have to remember all the names used everywhere to avoid all
- conflicts.
-
- If you want `first_side' to be visible to other programs, give it
- external linkage. To do that, define it outside all functions.
-
- <snip>
- DCM: i = malloc(1*sizeof(struct side));
-
- To use malloc, you must #include <stdlib.h> at the top of your file.
-
- <snip>
- DCM: }
- DCM:
- DCM:
- DCM: file3.c
- DCM: -------
- DCM:
- DCM: /* declare the structure side as extern, as in file1.c */
- DCM: extern struct side;
-
- Same problem as in file1.c
-
- DCM:
- DCM: /* insert a new element in the list */
- DCM: void
- DCM: store_side(struct side* i, struct side** first_side)
- DCM: {
- DCM: i->next = *first_side;
- DCM: *first_side = i;
- DCM: }
- DCM:
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-